package com.bitcat.firstapp.demo.netdemo

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {

    //请求的base_url
    private val BASE_URL = "http://10.0.2.2:8081/"

    //okhttp的日志拦截器
    private val interceptor = HttpLoggingInterceptor().apply {
        level = HttpLoggingInterceptor.Level.BODY
    }

    //创建okhttpClient
    private val okHttpClient = OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .build()

    //创建retrofit客户端
    private val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create()) //自动将JSON字符串转为kotlin对象
        .build()


    //请求的API
    val newsApi = retrofit.create(NewsApi::class.java)
}